home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / bin / linux-version < prev    next >
Encoding:
Text File  |  2011-07-26  |  2.6 KB  |  120 lines

  1. #!/usr/bin/perl
  2.  
  3. # Copyright 2011 Ben Hutchings
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  18.  
  19. use strict;
  20. use warnings;
  21.  
  22. use DebianLinux qw(version_cmp image_list);
  23.  
  24. sub usage {
  25.     my $fh = shift;
  26.     print $fh (<< "EOT");
  27. Usage: $0 compare VERSION1 OP VERSION2
  28.        $0 sort [--reverse] [VERSION1 VERSION2 ...]
  29.        $0 list [--paths]
  30.  
  31. The version arguments should be kernel version strings as shown by
  32. 'uname -r' and used in filenames.
  33.  
  34. The valid comparison operators are: lt le eq ge gt
  35. EOT
  36. }
  37.  
  38. sub usage_error {
  39.     usage(*STDERR{IO});
  40.     exit 2;
  41. }
  42.  
  43. sub compare_versions {
  44.     my %op_map = qw(lt < le <= eq == ge >= gt >);
  45.  
  46.     # Check arguments
  47.     if (@_ != 3) {
  48.     usage_error();
  49.     }
  50.     my ($left, $op, $right) = @_;
  51.     if (!exists($op_map{$op})) {
  52.     usage_error();
  53.     }
  54.  
  55.     my $sign = version_cmp($left, $right);
  56.     exit !eval("$sign ${op_map{$op}} 0");
  57. }
  58.  
  59. sub sort_versions {
  60.     # Check for --reverse option
  61.     my $sign = 1;
  62.     if (@_ >= 1 and $_[0] eq '--reverse') {
  63.     $sign = -1;
  64.     shift;
  65.     }
  66.  
  67.     # Collect versions from argv or stdin (with optional suffix after a space)
  68.     my @versions;
  69.     if (@_) {
  70.     @versions = map({[$_, "\n"]} @_);
  71.     } else {
  72.     while (<STDIN>) {
  73.         /^([^ ]*)(.*\n?)$/ or die;
  74.         push @versions, [$1, $2];
  75.     }
  76.     }
  77.  
  78.     for (sort({version_cmp($a->[0], $b->[0]) * $sign} @versions)) {
  79.     print @$_;
  80.     }
  81.     exit 0;
  82. }
  83.  
  84. sub list_versions {
  85.     my $show_paths;
  86.  
  87.     if (@_ == 1 and $_[0] eq '--paths') {
  88.     $show_paths = 1;
  89.     } elsif (@_ != 0) {
  90.     usage_error();
  91.     }
  92.  
  93.     for (image_list()) {
  94.     my ($version, $path) = @$_;
  95.     if ($show_paths) {
  96.         print "$version $path\n";
  97.     } else {
  98.         print "$version\n";
  99.     }
  100.     }
  101.     exit 0;
  102. }
  103.  
  104. if (@ARGV == 0) {
  105.     usage_error();
  106. }
  107.  
  108. my $command = shift;
  109. if ($command eq 'help' or grep({$_ eq '--help'} $command, @ARGV)) {
  110.     usage(*STDOUT{IO});
  111.     exit 0;
  112. } elsif ($command eq 'compare') {
  113.     compare_versions(@ARGV);
  114. } elsif ($command eq 'sort') {
  115.     sort_versions(@ARGV);
  116. } elsif ($command eq 'list') {
  117.     list_versions(@ARGV);
  118. }
  119. usage_error();
  120.